home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / sendmail / sendmail-5.65c+IDA-1.4.4.1 / src / convtime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-20  |  3.6 KB  |  181 lines

  1. /*
  2.  * Copyright (c) 1983 Eric P. Allman
  3.  * Copyright (c) 1988 Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms are permitted provided
  7.  * that: (1) source distributions retain this entire copyright notice and
  8.  * comment, and (2) distributions including binaries display the following
  9.  * acknowledgement:  ``This product includes software developed by the
  10.  * University of California, Berkeley and its contributors'' in the
  11.  * documentation or other materials provided with the distribution and in
  12.  * all advertising materials mentioning features or use of this software.
  13.  * Neither the name of the University nor the names of its contributors may
  14.  * be used to endorse or promote products derived from this software without
  15.  * specific prior written permission.
  16.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  17.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  18.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19.  */
  20.  
  21. #ifndef lint
  22. static char sccsid[] = "@(#)convtime.c    5.4 (Berkeley) 6/1/90";
  23. static char rcsid[] = "@(#)$Id: convtime.c,v 5.4.0.5 1991/06/21 12:40:29 paul Exp $";
  24. #endif /* not lint */
  25.  
  26. # include <ctype.h>
  27. # include "sendmail.h"
  28.  
  29. /*
  30. **  CONVTIME -- convert time
  31. **
  32. **    Takes a time as an ascii string with a trailing character
  33. **    giving units:
  34. **      s -- seconds
  35. **      m -- minutes
  36. **      h -- hours
  37. **      d -- days (default)
  38. **      w -- weeks
  39. **    For example, "3d12h" is three and a half days.
  40. **
  41. **    Parameters:
  42. **        p -- pointer to ascii time.
  43. **
  44. **    Returns:
  45. **        time in seconds.
  46. **
  47. **    Side Effects:
  48. **        none.
  49. */
  50.  
  51. TIME_TYPE
  52. convtime(p)
  53.     const char *p;
  54. {
  55.     register TIME_TYPE t, r;
  56.     register char c;
  57.  
  58.     r = 0;
  59.     while (*p != '\0')
  60.     {
  61.         t = 0;
  62.         while (isdigit(c = *p++))
  63.             t = t * 10 + (c - '0');
  64.         if (c == '\0')
  65.             p--;
  66.         switch (c)
  67.         {
  68.           case 'w':        /* weeks */
  69.             t *= 7;
  70.  
  71.           case 'd':        /* days */
  72.           default:
  73.             t *= 24;
  74.  
  75.           case 'h':        /* hours */
  76.             t *= 60;
  77.  
  78.           case 'm':        /* minutes */
  79.             t *= 60;
  80.  
  81.           case 's':        /* seconds */
  82.             break;
  83.         }
  84.         r += t;
  85.     }
  86.  
  87.     return (r);
  88. }
  89. /*
  90. **  PINTVL -- produce printable version of a time interval
  91. **
  92. **    Parameters:
  93. **        intvl -- the interval to be converted
  94. **        brief -- if TRUE, print this in an extremely compact form
  95. **            (basically used for logging).
  96. **
  97. **    Returns:
  98. **        A pointer to a string version of intvl suitable for
  99. **            printing or framing.
  100. **
  101. **    Side Effects:
  102. **        none.
  103. **
  104. **    Warning:
  105. **        The string returned is in a static buffer.
  106. */
  107.  
  108. # define PLURAL(n)    ((n) == 1 ? "" : "s")
  109.  
  110. char *
  111. pintvl(intvl, brief)
  112.     TIME_TYPE intvl;
  113.     bool brief;
  114. {
  115.     static char buf[256];
  116.     register char *p;
  117.     int wk = 0;
  118.     int dy, hr, mi, se;
  119.  
  120.     if (intvl == 0 && !brief)
  121.         return ("zero seconds");
  122.  
  123.     /* decode the interval into weeks, days, hours, minutes, seconds */
  124.     se = intvl % 60;
  125.     intvl /= 60;
  126.     mi = intvl % 60;
  127.     intvl /= 60;
  128.     hr = intvl % 24;
  129.     intvl /= 24;
  130.     if (brief)
  131.         dy = intvl;
  132.     else
  133.     {
  134.         dy = intvl % 7;
  135.         intvl /= 7;
  136.         wk = intvl;
  137.     }
  138.  
  139.     /* now turn it into a sexy form */
  140.     p = buf;
  141.     if (brief)
  142.     {
  143.         if (dy > 0)
  144.         {
  145.             (void) sprintf(p, "%d+", dy);
  146.             p += strlen(p);
  147.         }
  148.         (void) sprintf(p, "%02d:%02d:%02d", hr, mi, se);
  149.         return (buf);
  150.     }
  151.  
  152.     /* use the verbose form */
  153.     if (wk > 0)
  154.     {
  155.         (void) sprintf(p, ", %d week%s", wk, PLURAL(wk));
  156.         p += strlen(p);
  157.     }
  158.     if (dy > 0)
  159.     {
  160.         (void) sprintf(p, ", %d day%s", dy, PLURAL(dy));
  161.         p += strlen(p);
  162.     }
  163.     if (hr > 0)
  164.     {
  165.         (void) sprintf(p, ", %d hour%s", hr, PLURAL(hr));
  166.         p += strlen(p);
  167.     }
  168.     if (mi > 0)
  169.     {
  170.         (void) sprintf(p, ", %d minute%s", mi, PLURAL(mi));
  171.         p += strlen(p);
  172.     }
  173.     if (se > 0)
  174.     {
  175.         (void) sprintf(p, ", %d second%s", se, PLURAL(se));
  176.         /* p += strlen(p);    unused -pbp */
  177.     }
  178.  
  179.     return (buf + 2);
  180. }
  181.